package com.example.springsecurity.service.impl;

import com.example.springsecurity.domain.*;
import com.example.springsecurity.mapper.CollegeMapper;
import com.example.springsecurity.mapper.MajorMapper;
import com.example.springsecurity.mapper.RoleMapper;
import com.example.springsecurity.mapper.TbClassMapper;
import com.example.springsecurity.service.CollegeService;
import com.example.springsecurity.utils.ResultBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Service
public class CollegeServiceImpl implements CollegeService {

    @Autowired
    public CollegeMapper collegeMapper;

    @Autowired
    public MajorMapper majorMapper;

    @Autowired
    public TbClassMapper tbClassMapper;

    @Autowired
    public RoleMapper roleMapper;

    /**
     * 获取所有学院信息
     * @return
     */
    @Override
    public ResultBean getCollege() {
        List<College> collegeList=collegeMapper.getCollege();
        ResultBean resultBean=new ResultBean();
        resultBean.put("data",collegeList);
        resultBean.put("code",200);
        return resultBean;
    }

    /**
     * 根据学院id获取专业
     * @param collegeId
     * @return
     */
    @Override
    public ResultBean getMajor(Integer collegeId) {
        List<Major> majorList=collegeMapper.getMajor(collegeId);
        ResultBean resultBean=new ResultBean();
        resultBean.put("data",majorList);
        resultBean.put("code",200);
        return resultBean;
    }


    /**
     * 获取学院、专业、班级
     * @return
     */
    @Override
    public ResultBean getCollegeMajorClass() {
        /**
         * 获取内容
         */
        List<College> colleges = collegeMapper.getCollege();
        List<Major> majors = majorMapper.getAllMajor();
        List<TbClass> tbClasses = tbClassMapper.getAllTbClass();
        Set<Role> roles = roleMapper.selectAllRole();
        ResultBean resultBean=new ResultBean();
        resultBean.put("college",colleges);
        resultBean.put("major",majors);
        resultBean.put("tbClass",tbClasses);
        resultBean.put("role",roles);
        resultBean.put("code",200);
        /**
         * 循环遍历
         *//*
        for(int i=0;i<majors.size();i++){
            List<TbClass> tbClasses1=new ArrayList<>();
            for(int j=0;j<tbClasses.size();j++){
                if(tbClasses.get(j).getMajorId().equals(majors.get(i).getMajorId())){
                    tbClasses1.add(tbClasses.get(j));
                }
            }
            majors.get(i).setTbClassList(tbClasses1);
        }
        for(int i=0;i<colleges.size();i++){
            List<Major> majors1 = new ArrayList<>();
            for(int j=0;j<majors.size();j++){
                if(majors.get(j).getCollegeId().equals(colleges.get(i).getCollegeId())){
                    majors1.add(majors.get(j));
                }
            }
            colleges.get(i).setMajorList(majors1);
        }
        *//**
         * 获取角色信息
         *//*
        resultBean.put("he",colleges);*/
        return resultBean;
    }


    @Override
    public ResultBean getPrincipal(Integer collegeId) {
        List<SysUser> sysUsers=collegeMapper.getPrincipal(collegeId);
        ResultBean resultBean=new ResultBean();
        resultBean.put("data",sysUsers);
        resultBean.put("code",200);
        return resultBean;
    }


    /**
     * 根据学院id,专业id,班级id去获取学生用户信息
     * @param collegeId   学院id
     * @param majorId     专业id
     * @param tbClassId   班级id
     * @return
     */
    @Override
    public List<SysUser> getStudent(Integer collegeId, Integer majorId, Integer tbClassId) {
        return collegeMapper.getStudent(collegeId,majorId,tbClassId);
    }

    @Override
    public String getClassNameByUserName(String userName) {
        return collegeMapper.getClassNameByUserName(userName);
    }

    /**
     * 添加学院信息
     * @param college
     * @return
     */
    @Override
    public boolean insertCollege(College college) {
        return collegeMapper.insertCollege(college);
    }

    /**
     * 修改学院信息
     * @param college
     * @return
     */
    @Override
    public boolean updateCollege(College college) {
        return collegeMapper.updateCollege(college);
    }


    /**
     * 添加专业信息
     * @param major
     * @return
     */
    @Override
    public boolean insertMajor(Major major) {
        return majorMapper.insertMajor(major);
    }

    /**
     * 修改专业信息
     * @param major
     * @return
     */
    @Override
    public boolean updateMajor(Major major) {
        return majorMapper.updateMajor(major);
    }

    /**
     * 添加班级信息
     * @param tbClass
     * @return
     */
    @Override
    public boolean insertClass(TbClass tbClass) {
        return tbClassMapper.insertClass(tbClass);
    }

    /**
     * 修改班级信息
     * @param tbClass
     * @return
     */
    @Override
    public boolean updateClass(TbClass tbClass) {
        return tbClassMapper.updateClass(tbClass);
    }

    /**
     * 获取所有学院信息
     * @return
     */
    @Override
    public List<College> getColleges() {
        return collegeMapper.getCollege();
    }

    /**
     * 获取所有班级信息
     * @return
     */
    @Override
    public List<TbClass> getTbClasses() {
        return tbClassMapper.getAllTbClass();
    }
}
